<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
            "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>



<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="hevea 1.08">
<LINK rel="stylesheet" type="text/css" href="tutorial.css">
<TITLE>
Incomplete Tree Search
</TITLE>
</HEAD>
<BODY >
<A HREF="tutorial088.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="tutorial086.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="tutorial090.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<HR>

<H2 CLASS="section"><A NAME="htoc170">12.3</A>&nbsp;&nbsp;Incomplete Tree Search</H2><UL>
<LI><A HREF="tutorial089.html#toc83">First Solution</A>
<LI><A HREF="tutorial089.html#toc84">Bounded Backtrack Search</A>
<LI><A HREF="tutorial089.html#toc85">Depth Bounded Search</A>
<LI><A HREF="tutorial089.html#toc86">Credit Search</A>
<LI><A HREF="tutorial089.html#toc87">Timeout</A>
<LI><A HREF="tutorial089.html#toc88">Limited Discrepancy Search</A>
</UL>

<A NAME="@default327"></A>

The library <TT>ic_search</TT> contains a flexible
search routine
<B>search/6</B><A NAME="@default328"></A>,
which implements several variants of incomplete tree search.<BR>
<BR>
For demonstration, we will use the N-queens problem from above.
The following use of search/6 is equivalent to labeling(Xs) and
will print all 92 solutions:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
?-  queens(8, Xs),
    search(Xs, 0, input_order, indomain, complete, []),
    writeln(Xs),
    fail.
[1, 5, 8, 6, 3, 7, 2, 4]
...
[8, 4, 1, 3, 6, 2, 7, 5]
No.
</PRE></BLOCKQUOTE>
<A NAME="toc83"></A>
<H3 CLASS="subsection"><A NAME="htoc171">12.3.1</A>&nbsp;&nbsp;First Solution</H3>

One of the easiest ways to do incomplete search is to simply stop after
the first solution has been found. This is simply programmed using cut or
<A NAME="@default329"></A>
<A NAME="@default330"></A>
once/1:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
?-  queens(8, Xs),
    once search(Xs, 0, input_order, indomain, complete, []),
    writeln(Xs),
    fail.
[1, 5, 8, 6, 3, 7, 2, 4]
No.
</PRE></BLOCKQUOTE>
This will of course not speed up the finding of the first solution.<BR>
<BR>
<A NAME="toc84"></A>
<H3 CLASS="subsection"><A NAME="htoc172">12.3.2</A>&nbsp;&nbsp;Bounded Backtrack Search</H3> 
<A NAME="@default331"></A> 

Another way to limit the scope of backtrack search is to keep a
record of the number of backtracks, and curtail the search when this
limit is exceeded.
<BLOCKQUOTE CLASS="figure"><DIV CLASS="center"><HR WIDTH="80%" SIZE=2></DIV>
<DIV CLASS="center">
<IMG SRC="tutorial038.gif">
</DIV>
<BR>
<BR>
<DIV CLASS="center">Figure 12.9: Bounded-backtrack search</DIV><BR>
<BR>

<A NAME="figbbs"></A>
<DIV CLASS="center"><HR WIDTH="80%" SIZE=2></DIV></BLOCKQUOTE>
The <EM>bbs</EM> option of the search/6 predicate implements this:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
?-  queens(8, Xs),
    search(Xs, 0, input_order, indomain, bbs(20), []),
    writeln(Xs),
    fail.
[1, 5, 8, 6, 3, 7, 2, 4]
[1, 6, 8, 3, 7, 4, 2, 5]
[1, 7, 4, 6, 8, 2, 5, 3]
[1, 7, 5, 8, 2, 4, 6, 3]
No.
</PRE></BLOCKQUOTE>
Only the first 4 solutions are found, the next solution would have
required more backtracks than were allowed. 
Note that the solutions that are found are all located on the left hand
side of the search tree. This often makes sense because with a good
search heuristic, the solutions tend to be towards the left hand side.
Figure <A HREF="#figbbs">12.9</A> illustrates the effect of bbs (note that the diagram
does not correspond to the queens example, it shows an unconstrained search
tree with 5 binary variables).<BR>
<BR>
<A NAME="toc85"></A>
<H3 CLASS="subsection"><A NAME="htoc173">12.3.3</A>&nbsp;&nbsp;Depth Bounded Search</H3>
<A NAME="@default332"></A> 

A simple method of limiting search is to limit the depth of the search
tree. In many constraint problems with a fixed number of variables
this is not very useful, since all solutions occur at the same depth
of the tree. However, one may want to explore the tree completely
up to a certain depth and switch to an incomplete search method below this
depth. The search/6 predicate allows for instance the combination of
depth-bounded search with bounded-backtrack search. The following
explores the first 2 levels of the search tree completely, and does not
allow any backtracking below this level.
<BLOCKQUOTE CLASS="figure"><DIV CLASS="center"><HR WIDTH="80%" SIZE=2></DIV>
<DIV CLASS="center">
<IMG SRC="tutorial039.gif">
</DIV>
<BR>
<BR>
<DIV CLASS="center">Figure 12.10: Depth-bounded, combined with bounded-backtrack search</DIV><BR>
<BR>

<A NAME="figdbsbbs"></A>
<DIV CLASS="center"><HR WIDTH="80%" SIZE=2></DIV></BLOCKQUOTE>
This gives 16 solutions, equally distributed over the search tree:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
?-  queens(8, Xs),
    search(Xs, 0, input_order, indomain, dbs(2,bbs(0)), []),
    writeln(Xs),
    fail.
[3, 5, 2, 8, 1, 7, 4, 6]
[3, 6, 2, 5, 8, 1, 7, 4]
[4, 2, 5, 8, 6, 1, 3, 7]
[4, 7, 1, 8, 5, 2, 6, 3]
[4, 8, 1, 3, 6, 2, 7, 5]
[5, 1, 4, 6, 8, 2, 7, 3]
[5, 2, 4, 6, 8, 3, 1, 7]
[5, 3, 1, 6, 8, 2, 4, 7]
[5, 7, 1, 3, 8, 6, 4, 2]
[6, 4, 1, 5, 8, 2, 7, 3]
[7, 1, 3, 8, 6, 4, 2, 5]
[7, 2, 4, 1, 8, 5, 3, 6]
[7, 3, 1, 6, 8, 5, 2, 4]
[8, 2, 4, 1, 7, 5, 3, 6]
[8, 3, 1, 6, 2, 5, 7, 4]
[8, 4, 1, 3, 6, 2, 7, 5]
No (0.18s cpu)
</PRE></BLOCKQUOTE>
<A NAME="toc86"></A>
<H3 CLASS="subsection"><A NAME="htoc174">12.3.4</A>&nbsp;&nbsp;Credit Search</H3>
<A NAME="@default333"></A>

Credit search[<A HREF="tutorial133.html#beldiceanu:credit"><CITE>1</CITE></A>]
is a tree search method where the number of
nondeterministic choices is limited a priori. This is achieved by
starting the search at the tree root with a certain integral amount of
credit. This credit is split between the child nodes, their credit
between their child nodes, and so on. A single unit of credit cannot
be split any further: subtrees provided with only a single credit unit
are not allowed any nondeterministics choices, only one path though these
subtrees can be explored, i.e. only one leaf in the subtree can be visited.
Subtrees for which no credit is left are pruned,
i.e. not visited.<BR>
<BR>
The following code (a replacement for labeling/1)
implements credit search. For ease of understanding, it is
limited to boolean variables:

	<TABLE CELLPADDING=10>
<TR><TD BGCOLOR="#CCCCFF">
	<BLOCKQUOTE CLASS="quote"><PRE>
% Credit search (for boolean variables only)
credit_search(Credit, Xs) :-
        (
            foreach(X, Xs),
            fromto(Credit, ParentCredit, ChildCredit, _)
        do
            ( var(X) -&gt;
                ParentCredit &gt; 0,  % possibly cut-off search here
                ( % Choice
                    X = 0, ChildCredit is (ParentCredit+1)//2
                ;
                    X = 1, ChildCredit is ParentCredit//2
                )
            ;
                ChildCredit = ParentCredit
            )
        ).
</PRE></BLOCKQUOTE></TD>
</TR></TABLE>
Note that the leftmost alternative (here X=0)
gets slightly more credit than the rightmost one (here X=1)
by rounding the child node's credit up rather than down. 
This is especially relevant when the leftover credit is down to 1:
from then on, only the leftmost alternatives will be taken until a
leaf of the search tree is reached. The leftmost alternative should
therefore be the one favoured by the search heuristics.<BR>
<BR>
What is a reasonable amount of credit to give to a search?
In an unconstrained search tree, the credit is equivalent to the
number of leaf nodes that will be reached.
The number of leaf nodes grows exponentially with the number of
labelled variables, while tractable computations should have
polynomial runtimes. A good rule of thumb could therefore be to
use as credit the number of variables squared or cubed, thus enforcing
polynomial runtime.<BR>
<BR>
Note that this method in its pure form allows choices only close to the
root of the search tree, and disallows choices completely below a certain
tree depth. This is too restrictive when the value selection strategy
is not good enough. A possible remedy is to combine credit search with
bounded backtrack search.<BR>
<BR>
The implementation of credit search in the search/6 predicate works for
arbitrary domain variables: Credit is distributed by giving half to the
leftmost child node, half of the remaining credit to the second child node
and so on. Any remaining credit after the last child node is lost.
<BLOCKQUOTE CLASS="figure"><DIV CLASS="center"><HR WIDTH="80%" SIZE=2></DIV>
<DIV CLASS="center">
<IMG SRC="tutorial040.gif">
</DIV>
<BR>
<BR>
<DIV CLASS="center">Figure 12.11: Credit-based incomplete search</DIV><BR>
<BR>

<A NAME="figcredit"></A>
<DIV CLASS="center"><HR WIDTH="80%" SIZE=2></DIV></BLOCKQUOTE>
In this implementation, credit search is always combined with another
search method which is to be used when the credit runs out.<BR>
<BR>
When we use credit search in the queens example, we get a limited number
of solutions, but these solutions are not the leftmost ones (like with
bounded-backtrack search), they are
from different parts of the search tree, although biased towards the left:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
?- queens(8, Xs),
   search(Xs, 0, input_order, indomain, credit(20,bbs(0)), []),
   writeln(Xs),
   fail.
[2, 4, 6, 8, 3, 1, 7, 5]
[2, 6, 1, 7, 4, 8, 3, 5]
[3, 5, 2, 8, 1, 7, 4, 6]
[5, 1, 4, 6, 8, 2, 7, 3]
No.
</PRE></BLOCKQUOTE>
We have used a credit limit of 20. When credit runs out, we switch to
bounded backtrack search with a limit of 0 backtracks.<BR>
<BR>
<A NAME="toc87"></A>
<H3 CLASS="subsection"><A NAME="htoc175">12.3.5</A>&nbsp;&nbsp;Timeout</H3>
<A NAME="@default334"></A>

Another form of incomplete tree search is simply to use time-outs.
<A NAME="@default335"></A>
<A NAME="@default336"></A>
The branch-and-bound primitives <TT>bb_min/3,6</TT> allow
a maximal runtime to be specified. If a timeout occurs, the best solution
found so far is returned instead of the proven optimum.<BR>
<BR>
A general timeout is available from the library <TT>test_util</TT>.
It has parameters <TT>timeout(Goal, Seconds, TimeOutGoal)</TT>.
When <TT>Goal</TT> has run for
more than <TT>Seconds</TT> seconds, it is aborted and <TT>TimeOutGoal</TT>
is called instead. <BR>
<BR>
<A NAME="toc88"></A>
<H3 CLASS="subsection"><A NAME="htoc176">12.3.6</A>&nbsp;&nbsp;Limited Discrepancy Search</H3>
<A NAME="@default337"></A>

Limited discrepancy search (<EM>LDS</EM>) is a search method that assumes
the user has a good heuristic for directing the search. A perfect
heuristic would, of course, not require any search. However most
heuristics are occasionally misleading. Limited Discrepancy Search
follows the heuristic on almost every decision. The
&#8220;discrepancy&#8221; is a measure of the degree to which it fails to follow
the heuristic. LDS starts searching with a discrepancy of 0 (which
means it follows the heuristic exactly). Each time LDS fails to find
a solution with a given discrepancy, the discrepancy is increased and
search restarts. In theory the search is complete, as eventually the
discrepancy will become large enough to admit a solution, or cover
the whole search space. In practice, however, it is only beneficial
to apply LDS with small discrepancies. Subsequently, if no solution
is found, other search methods should be tried.
The definitive reference to LDS is [<A HREF="tutorial133.html#harvey95:lds:inp"><CITE>29</CITE></A>]
<BR>
<BR>
<A NAME="@default338"></A>
There are different possible ways of measuring discrepancies.
The one implemented in the search/6 predicate is a variant of the
original proposal. It considers the first
value selection choice as the heuristically best value with
discrepancy 0, the first alternative has a discrepancy of 1, the
second a discrepancy of 2 and so on.
<BLOCKQUOTE CLASS="figure"><DIV CLASS="center"><HR WIDTH="80%" SIZE=2></DIV>
<DIV CLASS="center">
<IMG SRC="tutorial041.gif">
</DIV>
<BR>
<BR>
<DIV CLASS="center">Figure 12.12: Incomplete search with LDS</DIV><BR>
<BR>

<A NAME="figlds"></A>
<DIV CLASS="center"><HR WIDTH="80%" SIZE=2></DIV></BLOCKQUOTE>
As LDS relies on a good heuristic, it only makes sense for the queens
problem if we use a good heuristic, e.g. first-fail variable selection
and indomain-middle value selection. Allowing a discrepancy of 1 yields
4 solutions:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
?- queens(8, Xs), 
   search(Xs, 0, first_fail, indomain_middle, lds(1), []),
   writeln(Xs),
   fail.
[4, 6, 1, 5, 2, 8, 3, 7]
[4, 6, 8, 3, 1, 7, 5, 2]
[4, 2, 7, 5, 1, 8, 6, 3]
[5, 3, 1, 6, 8, 2, 4, 7]
No.
</PRE></BLOCKQUOTE>
The reference also suggests that combining LDS with Bounded Backtrack
Search (<EM>BBS</EM>) yields good behaviour. The search/6 predicate
accordingly supports the combination of LDS with BBS and DBS.
The rationale for this is that heuristic choices typically get
more reliable deeper down in the search tree.<BR>
<BR>
<HR>
<A HREF="tutorial088.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="tutorial086.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="tutorial090.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
</BODY>
</HTML>
